include<BOSL2/std.scad>
include<BOSL2/gears.scad>
include<tools/util.scad>
$fn=100;

mod=1.5; //模数
 backing=3; //冠齿轮齿下圆盘厚度
 PA=20;//压力角
 face=15;//厚度
 gap=2;//空隙
 
 //4x5x5,15:60 15:75 15:75
cteeth=60; //齿数
pteeth=15;//齿数
dteeth=75;
bteeth=26;
//半径们
pr_60 = pitch_radius(mod=mod, teeth=cteeth);
pr_15 = pitch_radius(mod=mod, teeth=pteeth);
pr_75 = pitch_radius(mod=mod, teeth=dteeth);
pr_26 = pitch_radius(mod=mod, teeth=bteeth);

//translate([0,0,-(pr_60+pr_15)])moto_gear();
//spur_gear_60();
//rod_1();
//translate([face+gap,0,0])spur_gear_15();
//translate([face+gap,-(pr_15+pr_75),0])spur_gear_75();
//translate([0,-(pr_15+pr_75),0])rod_1();
//translate([0,-(pr_15+pr_75),0])spur_gear_15();
//translate([0,-(pr_75*2+pr_15*2),0])spur_gear_75();
//translate([0,-(pr_75*2+pr_15*2),0])rod_2();
shell();
/*translate([(face*4.5+3*gap+6+20),-(pr_75*2+pr_15*2),0])
{
        pair_of_bevel_gear();
    
       translate([0,0,-pr_26-2])rod_3();
       translate([0,0,-pr_26 -2-48.5+8])pair_gear_26_bearing();
       translate([0,0,-pr_26 -2-48+8-12])cube([9.6,9.6,23],center=true);
}
*/

//translate([100,0,0])stick_std();
//translate([130,0,0])moto_axis();
//出图用
//pair_gear_26_bearing();
//cube([9.6,9.6,23],center=true);
module moto_gear(){
    rotate([0,-90,0]) {
            spur_gear(mod=mod, teeth=pteeth,pressure_angle=PA, thickness=face,gear_spin=270/pteeth);
            translate([0,0,face/2])difference(){
                    cylinder(h=40,d=12);
                    translate([0,0,37])moto_axis_mask();
                    }
         }

}
module moto_axis_mask(){
    intersection(){
            cube([5.6,3.9,6.1],center=true);
            translate([0,0,-3])cylinder(h=6.1,d=5.6);
        }
} 
module moto_axis(){
    translate([0,0,5])difference(){
        minkowski(){
            cube([12,12,8],center=true);//14,14,10
            sphere(1);
        }
        translate([0,0,-1])cube([10,10,8],center=true);
        }
    translate([0,0,14])intersection(){
            cube([5.6,3.6,8],center=true);
            translate([0,0,-4])cylinder(h=8,d=5.4);
        }    
} 
module spur_gear_60(){
rotate([0,90,0])
    difference(){
    spur_gear(mod=mod, teeth=cteeth,pressure_angle=PA, thickness=face);
    cube([10,10,face],center=true);
    }
}


module spur_gear_15(){
    rotate([0,90,0])union(){
        difference(){
        spur_gear(mod=mod, teeth=pteeth,pressure_angle=PA, thickness=face);
        cube([10,10,face],center=true);
        }
        //打印用加强条
        translate([0,0,-face/2+0.1 ])cube([6,10,0.2],center=true);
    }
}
module spur_gear_75(){
    rotate([0,90,0])
    difference(){
    spur_gear(mod=mod, teeth=dteeth,pressure_angle=PA, thickness=face);
    cube([10,10,face],center=true);
    }
}
//1:1
module pair_of_bevel_gear(){

    difference(){
        bevel_gear(mod=mod, teeth=bteeth, mate_teeth=bteeth,slices=12, anchor="apex", orient=RIGHT);
        translate([-pr_26,0,0])cube([15,10,10],center=true);
    }
    difference(){
        bevel_gear(mod=mod, teeth=bteeth, mate_teeth=bteeth, right_handed=true,slices=12, anchor="apex", backing=3, spin=180/bteeth);
        translate([0,0,-pr_26])cube([10,10,15],center=true);
    }
}

module pair_gear_26_bearing(){
    difference(){
        d_bearing(r=30,height=16,ball_r=6,ball_num=8);
        translate([0,0,-1])cube([10,10,16],center=true);
    }
    translate([0,0,10.5])difference(){
        cube([56,20,5],center=true);
        translate([0,0,-2.5])cube([30,20,2],center=true);
        }
    translate([0,0,22.25])difference(){
            cube([20,20,18.5],center=true);
            cube([10,10,18.5],center=true);
    }
    
}
module rod_1(){
    difference(){
        union(){
            translate([0,-4.8,-4.8])cube([face*1.5+6+gap,9.6,9.6]);
            translate([-face*1.5,0,0])rotate([0,90,0])hull(){
                cylinder(h=face/2,d=9.6);
                translate([-4.8,-4.8,face/2])cube([9.6,9.6,face]);
            }
            translate([-(20+face*1.5),0,0])rotate([0,90,0])cylinder(h=20,d=9.6);
        }
        //定位孔
        translate([-(face/2+2.6),0,0])cube([5,5,9.7],center=true);
        translate([face*1.5+2.6+gap,0,0])cube([5,5,9.7],center=true);    
            translate([face/2+2.6+gap,0,0])cube([5,5,9.7],center=true);       
    }
    translate([face*2.5+gap+6,0,0])rotate([0,-90,0])hull(){
                cylinder(h=face/2,d=9.6);
                translate([-4.8,-4.8,face/2])cube([9.6,9.6,face/2]);
    }
    translate([face*2.5+gap+6,0,0])rotate([0,90,0])cylinder(h=20,d=9.6);
}

module rod_2(){
    rod_1();
    translate([face*2.5+gap+6+20,0,0])difference(){
        rotate([0,90,0])hull(){
                cylinder(h=face/2,d=9.6);
                translate([-4.8,-4.8,face/2])cube([9.6,9.6,face+gap]);
            }
            translate([face/2+gap,0,0])cube([5,5,9.7],center=true);
    }
}

module rod_3(){
    difference(){
        union(){
            cube([9.6,9.6,15],center=true);
            translate([0,0,8.5])hull(){
                cube([9.6,9.6,2],center=true);
                translate([0,0,1])cylinder(h=8,d=9.6);
            }
            translate([-4.8,-4.8,-27.5])cube([9.6,9.6,20]);
            translate([0,0,7.5])cylinder(h=50,d=9.6);
        }
        translate([0,0,-6])cube([5,9.7,5],center=true);
    }
    translate([0,0,57.5])hull(){
        cylinder(h=10,d=9.6);
        translate([0,0,25])cube([9.6,9.6,30],center=true);
}

}


module shell(){
    difference(){
        union(){
            translate([-35,-150,-90])cube([3.5,170,110]);
            translate([55,-150,-90])cube([3.5,170,110]);
            translate([-35,-150,-90])cube([160,40,3.5]);
            translate([-35,-20,-90])cube([90,40,3.5]);
        }
        translate([-40,0,0])rotate([0,90,0])cylinder(h=110,d=10.6);
        translate([-40,-(pr_15+pr_75),0])rotate([0,90,0])cylinder(h=110,d=10.6);
        translate([-40,-(pr_75*2+pr_15*2),0])rotate([0,90,0])cylinder(h=110,d=10.6);
        translate([-40,0,-(pr_60+pr_15)])rotate([0,90,0])cylinder(h=10,d=13);
        translate([52,-(pr_75*2+pr_15*2)-5.3,0])cube([10,10.6,40]);
    }
    translate([(face*4.5+3*gap+6+20),-(pr_75*2+pr_15*2),-80.5])difference(){
            cube([20,20,19],center=true);
            cube([10,10,19],center=true);
    }
    points=[[-32,15],[-32,-135],[58,-135]];
    for(pos=points){
        translate([pos[0],pos[1],-87])d__tri_stiff(x=10,y=25,z=4);
    }
   points_out=[[55,15],[55,-135]];
    for(pos=points_out){
        translate([pos[0],pos[1],-87])rotate([0,0,180])d__tri_stiff(x=10,y=25,z=4);
    }
}

module stick_std(){
    cube([4.6,4.6,11]);
}